﻿using System;
using System.Net;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using Sharp.DataTransferObject;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Communication;
using Sharp.Infrastructure.Search;
using Sharp.Platform.Mvc;
using Sharp.ServiceContracts;
using Sharp.Web.Models;
using PartialViewResult = Ext.Net.MVC.PartialViewResult;

namespace Sharp.Web.Controllers
{
    /// <summary>
    /// 员工
    /// </summary>
    [AutomaticTemplate]
    public class EmployeController : Controller
    {
        /// <summary>
        /// 入口
        /// </summary>
        /// <param name="containerId">容器Id</param>
        /// <returns></returns>
        public ActionResult Index(string containerId)
        {
            return new PartialViewResult
            {
                Model = new EmployeeIndexModel(),
                ContainerId = containerId,
                RenderMode = RenderMode.AddTo
            };
        }
        /// <summary>
        ///     分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(StoreRequestParameters parameters)
        {
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                var pageInfo = proxy.Channel.GetEmployeePaged(parameters.ToPagingFeature());

                return this.Store(pageInfo.Data, pageInfo.TotalRecords);
            }
        }

        public ActionResult GetEmployees(Guid[] orgids)
        {
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                var pageInfo = proxy.Channel.GetEmployeesInOrg(orgids);

                return this.Store(pageInfo);
            }
            //var pageInfo = new PagedResult<EmployeeDTO>();
            //pageInfo.Data = new System.Collections.Generic.List<EmployeeDTO>();

            //return this.Store(pageInfo.Data, pageInfo.TotalRecords);
        }


        // GET: Manage/Employe/Details/5
        public ActionResult Details(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            EmployeeDTO dto = null;
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetEmployeeInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }

            return new PartialViewResult { Model = dto.To<EmployeeModel>() };
        }

        // GET: Manage/Employe/Create
        public ActionResult Create()
        {
            return new PartialViewResult { Model = new EmployeeModel() };
        }


        // GET: Manage/User/Edit/5
        public ActionResult Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            EmployeeDTO dto = null;
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetEmployeeInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }


            return new PartialViewResult { Model = dto.To<EmployeeModel>() };
        }


        [HttpPost]
        public ActionResult Save(EmployeeModel user)
        {
            EmployeeDTO dto = user.To<EmployeeDTO>();

            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                proxy.Channel.SaveEmployee(dto);
            }

            return this.FormPanel();
        }

        [HttpPost]
        public JsonResult Delete(Guid[] ids)
        {
            var res = new JsonResult();
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                res.Data = proxy.Channel.DeleteEmployee(ids);
            }

            return res;
        }
    }
}
